Fusion Framework Http Module
The Fusion Framework HTTP Module provides a streamlined and powerful HTTP client for making requests to APIs.
The module supports both asynchronous (Promise-based) and observable (RxJS-based) execution methods, allowing you to choose the approach that best suits your use case.
It also offers advanced features, such as MSAL (Microsoft Authentication Library) integration, request and response operators, and response selectors, to enhance the functionality and flexibility of HTTP communication.
Whether you're building a simple application or a complex portal, the Fusion Framework HTTP Module equips you with the essential tools to handle HTTP requests efficiently and effectively. With its rich feature set and intuitive API, the module simplifies the process of working with HTTP clients, enabling you to focus on building robust and scalable applications.
Key Features:
- Streamlined API for easy configuration and operation of HTTP clients
- Integrated MSAL (Microsoft Authentication Library) support for robust authentication
- Unified management system for HTTP client configurations
- Factory method for client creation, ensuring optimal state management
- Capabilities for intercepting and modifying requests and responses
- Familiar syntax inspired by the Web's Fetch API
Package
Namespace | Description |
---|
@equinor/fusion-framework-module-http | http module |
@equinor/fusion-framework-module-http/client | http clients |
@equinor/fusion-framework-module-http/selectors | http selectors |
@equinor/fusion-framework-module-http/operators | http client operators |
@equinor/fusion-framework-module-http/errors | http client errors |
Usage
Working with Fusion Framework, HTTP clients are defined during the configuration phase. The configuration is called by the Framework during initialization, ensuring that the HTTP clients are ready to be used. This approach allows for centralized and optimized client configuration, promoting consistency, performance, and security in HTTP communication within the Fusion Framework ecosystem.
see Configuring HTTP Clients for more information.
const configure = (configurator: IModulesConfigurator) => {
configurator.http.configureClient('msalClient', {
baseUri: 'https://api.example.com',
defaultScopes: ['api://example-api/.default'],
});
}
When the application is initialized, the configuration is called, and the HTTP client becomes ready to be used.
see Working with HTTP clients for more information.
const client = modules.http.createClient('myClient');
client.json('/some-endpoint')
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Concepts
Pure fetch
The HTTP module extends the native fetch
API, providing a streamlined experience for making HTTP requests. It adds features and capabilities to simplify common tasks and ensure compatibility with existing code and libraries.
Observable
The HTTP module uses RxJS observables for handling HTTP requests and responses. Observables provide a flexible and powerful way to work with asynchronous data streams. They allow you to process and transform data in a declarative and composable manner. Unsubscribing from an observable automatically aborts the corresponding HTTP request, preventing unnecessary network traffic and ensuring efficient request management.
Defining Clients Upfront
In the Fusion Framework, HTTP clients are configured during initialization to ensure they are ready for use. This promotes centralized configuration, optimized performance, and enhances consistency, security, and modularity in HTTP communication.
Client factory
The HTTP module provides a client factory method for creating HTTP clients. This method allows you to specify the client's name and retrieve a configured instance. Using the client factory ensures clean and predictable client instances without any mutations from previous requests. This promotes a reliable state for each client, avoiding unexpected behavior caused by shared state.
Process operators
Process operators are functions that can be added to the HTTP client to customize its behavior and add additional functionality to the request/response lifecycle. They allow you to intercept and modify requests and responses, add custom headers, log data, validate responses, and more. By using process operators, you can extend the functionality of the HTTP client to meet your application's specific requirements.
Request execution
When executing HTTP requests (1), the internal fetch method will create an Observable
(2) from the provided the request options. The stream will then process the request handlers (3 - 5) before emitting the request to the HttpClient request Subject
(6).
Then a native fetch
call is made to the HTTP service (7). If the request is successful (17), the response is processed by the response handlers (18 - 20) before being emitted to the HttpClient response Subject
(21). The response is then processed by the response selector (22) before being emitted to the user (23).
In case of an error (17), the error is emitted to the user (18).
If the user calls abort on the client (8), or triggers provided AbortSignal
in call args, the observable will abort the request (10). fetch
will throw an abort error (11), which is emitted to the user (12).
If the user unsubscribes from the observable (13) or trigger provided AbortSignal
in call args, the request is aborted (14).
[!NOTE]
- When an error is emitted, the observable will complete.
- Observables only emit errors if they are subscribed to.
- fetch observables are cold, meaning they will close when request is completed or aborted.
[!IMPORTANT]
HttpClient stream functions will not execute until subscribed to.
sequenceDiagram
autonumber
actor User
participant Client
User->>Client: calls fetch method
create participant Observable
Client->>+Observable: create observable
Observable->>+Client: prepare request
loop
Client->>Client: process request handlers
end
Client-->>-Observable: request object
Observable-->>Client: emit request
create participant HTTPService as Http Service
Observable->>+HTTPService: fetch
break when aborted
User->>Client: abort requests
Client->>Observable: close observable
Observable->>HTTPService: abort request
HTTPService-->>Observable: Abort error
Observable--xUser: emit error
end
break when unsubscribe
User--xObservable: unsubscribe
Observable->>HTTPService: abort request
end
alt is request error
HTTPService-->>Observable: error
Observable--xUser: emit error
end
HTTPService-->>-Observable: response
Observable-->>+Client: prepare response
loop
Client->>Client: process response handlers
end
Client-->>-Observable: response object
Observable-->>Client: emit response
opt
Observable->>Observable: apply response selector
end
Observable--x-User: emit result
Configuring HTTP Clients
Configuring HTTP clients before the application renders is crucial for several reasons:
-
Centralized Configuration: By configuring clients upfront, we establish a single source of truth for all HTTP client settings. This centralization simplifies the management and maintenance of client configurations throughout the application.
-
Performance Optimization: Pre-configuring clients improves performance by ensuring they are ready to use when needed, eliminating on-the-fly configuration that can slow down API calls.
-
Consistency: Configuring clients before rendering ensures consistent usage of client configurations across the application, promoting uniformity in API call implementation.
-
Environment-specific Settings: This approach facilitates easy configuration of environment-specific settings, such as different base URLs for development, staging, and production environments.
-
Security: Configuring clients before rendering enables the setup of essential security measures, including authentication handlers and default scopes, ensuring secure API calls from the outset.
-
Modularity: This configuration approach supports a modular architecture, allowing different modules to define their own HTTP client configurations, which can then be combined before rendering the application.
By following this approach, you can enhance the efficiency, consistency, security, and modularity of your application's HTTP communication within the Fusion Framework ecosystem.
Configuration Options
When configuring an HTTP client in the Fusion Framework, you can specify various settings to customize its behavior. The following options are available for configuring an HTTP client:
Property | Description |
---|
baseUri | The base URI for the API endpoint. |
defaultScopes | The default scopes for MSAL authentication. |
selector | The response selector for processing the response. |
onCreate | A callback function to execute when the client is created. |
requestHandler | The request operators for processing outgoing requests. |
responseHandler | The response operators for processing incoming responses. |
ctor | The constructor function for a custom client. |
Basic configuration
To configure a basic HTTP client in the Fusion Framework, you can use the following example. This setup is ideal for simple API consumption that does not require advanced authentication mechanisms.
const configure = (configurator: IModulesConfigurator) => {
configurator.http.configureClient(
'myClient',
'https://api.example.com'
);
}
Configuration with MSAL Authentication
For consuming APIs that require secure authentication, it is recommended to configure an HTTP client with MSAL (Microsoft Authentication Library). The following example demonstrates how to set up an HTTP client with MSAL authentication, specifying a default scope.
const configure = (configurator: IModulesConfigurator) => {
configurator.http.configureClient('msalClient', {
baseUri: 'https://api.example.com',
defaultScopes: ['api://example-api/.default'],
});
}
Advanced Configuration
Configuring a http client with callback
You can configure an HTTP client using a callback function, which allows for more dynamic and flexible configuration. Here's an example:
const configure = (configurator: IModulesConfigurator) => {
configurator.http.configureClient('callbackClient', (client) => {
client.uri = 'https://api.example.com';
client.requestHandler.add('logger', (request) => {
console.log('Outgoing request:', request);
});
client.responseHandler.add('errorChecker', (response) => {
if (!response.ok) {
console.error('Error in response:', response.status, response.statusText);
switch(response.status) {
case 401:
break;
case 500:
break;
default:
break;
}
}
});
client.defaultScopes = ['api://example-api/.default'];
});
}
Configuration with custom client
You can also configure an HTTP client with a custom client instance. This approach allows you to define a custom client with specific settings and behaviors tailored to your application's requirements.
class CustomHttpClient implements IHttpClient {
}
const configure = (configurator: IModulesConfigurator) => {
configurator.http.configureClient('custom', {
ctor: CustomHttpClient,
baseUri: 'https://api.example.com',
});
}
Working with HTTP clients
To create an HTTP client, you can use the createClient
method provided by the module. This method takes the name of the client you want to create and returns an instance of the HTTP client configured with the specified settings:
const msalClient = modules.http.createClient('msalClient');
Once you have created a client, you can use it to make requests to APIs. The client provides several methods for executing HTTP requests, such as fetch
, json
, and blob
, which return Promises, and fetch$
, json$
, and blob$
, which return observables.
Async vs Observable Execution
The Fusion Framework HTTP module offers developers the flexibility to choose between asynchronous (Promise-based) and observable (RxJS-based) methods for executing HTTP requests. This allows developers to select the approach that best suits their specific use case.
It's worth noting that both approaches utilize the same underlying RxJS-based implementation, ensuring consistent behavior regardless of the chosen execution method.
[!TIP]
While async methods are more familiar to many developers, observable methods offer additional flexibility and power when dealing with complex data flows or when fine-grained control over the request lifecycle is needed.
Async Execution
For simpler use cases, the module also exposes async methods that return Promises:
msalClient.json<MyDataType>('/api/data')
.then(data => console.log('Received data:', data))
.catch(error => console.error('An error occurred:', error))
.finally(() => console.log('Async operation completed'));
Async execution is beneficial for:
- Simpler, more familiar syntax for many developers
- Easy integration with async/await patterns
- Straightforward error handling with try/catch
Observable Execution
The module uses RxJS observables at its core, which provides powerful stream processing capabilities. Observable methods are denoted with a $
suffix:
msalClient.json$<MyDataType>('/api/data').subscribe({
next: (data) => console.log('Received data:', data),
error: (error) => console.error('An error occurred:', error),
complete: () => console.log('Observable completed'),
});
Observable execution is particularly useful for:
- Handling real-time data streams
- Implementing complex data transformations
- Cancelling ongoing requests
Working with MSAL
When working with MSAL authentication, you can specify the required scopes for each request. By default, the client will use the defaultScopes
configured for the HTTP client. However, you can also overload the scopes for individual API calls by providing an array of scopes in the request options.
[!IMPORTANT]
By default the module will add a request operator which will acquire a token from MSAL before the request is sent. This token is added as a bearer token in the request header.
[!WARNING]
The scopes provided in the request options will override the default scopes configured for the client.
msalClient.json('/some-endpoint', {
scopes: ['api://example-api/.admin']
})
Working with JSON
To interact with JSON APIs, you can use the json
or json$
methods.
- The
json$
method returns an Observable that emits the parsed JSON data. - The
json
method returns a promise that resolves when the first value from json$
is emitted. - Both methods automatically parse the response as JSON.
- They also add standard headers to the request.
- The response is returned as a
JsonResponse<T>
.
[!TIP]
Use typed responses to ensure that you're using the correct types.
[!IMPORTANT]
- Request Body: Will only use
JSON.stringify
, pre-process data if needed. - Response Body: Will only use
response.json()
, post-process data if needed.
const data = {
id: 1,
name: 'John Doe',
}
client.json('/some-endpoint', { method: "PATCH", body: data });
client.json$('/some-endpoint', { method: "PATCH", body: data })
Handling Errors
When working with async functions and observables, it's important to handle errors properly! The HTTP client provides two custom error types for handling HTTP-related errors:
HttpResponseError
: This error is thrown when there's an issue with the HTTP response. It includes the original response object, allowing you to access additional details about the error.
HttpJsonResponseError
: This error is used when there's an issue parsing JSON data from the response. It includes the parsed data (if available) in addition to the response object.
When using the HTTP client, you can catch these errors and handle them appropriately:
const processError = (error: unknown) => {
if (error instanceof HttpJsonResponseError) {
console.error('JSON Response Error:', error.message, error.data);
} else if (error instanceof HttpResponseError) {
console.error('HTTP Response Error:', error.message, error.response);
} else {
console.error('Unknown error:', error);
}
}
try { const data = await client.json('/some-endpoint'); }
catch (error) { processError(error) }
finally { }
client.json('/some-endpoint').subscribe({
next: (response) => { },
error: processError,
complete: () => { }
});
[!IMPORTANT]
Example only logs the error, your application should handle errors appropriately based on the use case.
Use typed responses
When coding in TypeScript and using the HTTP client, it's important to use typed responses. This helps ensure that you handle errors correctly and that you're using the correct types.
interface User {
id: number;
name: string;
}
const getUser = (id: number): Promise<User> => client.json<User>(`/users/${id}`);
Using Response Selectors
A good practice when working with data returned from an API is to use reusable selectors. This helps ensure that you don't have to write the same code over and over again.
Selectors are functions that process and transform the raw HTTP response before it's returned to your application. The HTTP client provides built-in selectors for common use cases, such as JSON parsing and blob handling.
[!TIP]
When providing selectors, the response type is inferred based on the selector's return type.
Here is an example of a selector that checks if a resource exists and a selector that parses a CSV file:
export const resourceExistsSelector = (response: Response): boolean => {
if(response.ok) {
return true;
} else if(response.status === 404) {
return false;
}
throw Error(`Unexpected response status: ${response.status}`);
}
export type CsvData = string[][];
export const csvSelector: ResponseSelector = async (response: Response): Promise<CsvData> => {
const text = await response.text();
return text.split('\n').map(line => line.split(','));
};
Here is an example of how to use the selectors in your application:
import { resourceExistsSelector, csvSelector, type CsvData } from './selectors';
export const hasResource = (client: HttpClient, itemId: string): Promise<boolean> => {
return client.fetch(`/resource/${itemId}`, {
method: 'HEAD',
selector: resourceExistsSelector
});
}
export const getCSVData = (client: HttpClient, filename: string): Promise<CsvData> => {
return client.fetch(filename, { selector: csvSelector });
}
Reusing Selectors
You can reuse selectors across different parts of your application. This helps ensure that you don't have to write the same code multiple times.
import { jsonSelector } from '@equinor/fusion-framework-module-http/selectors';
import { schema, SchemaType } from './schema';
export const dataParserSelector: ResponseSelector<SchemaType> => async(response) => {
const rawData = await jsonSelector(response);
return schema.parse(rawData);
}
Observable Selectors
The ResponseSelector
type is a generic type that allows you to return ObservableInput
types. This means that the selector supports Promises, Observables, AsyncIterables, and other types that implement the ObservableInput
interface.
import { from } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { jsonSelector } from '@equinor/fusion-framework-module-http/selectors';
import { schema, SchemaType } from './schema';
export const dataParserSelector: ResponseSelector => (response): Observable<SchemaType> => {
return from(jsonSelector(response)).pipe(
switchMap(schema.parseAsync)
);
}
Use the abort functionality for cancellable requests
When using the HTTP client, it's important to use the abort functionality for cancellable requests. This helps ensure that you don't have to handle the cancellation yourself.
useEffect(() => {
const abortController = new AbortController();
try {
client.fetch(
'/long-running-operation',
{
signal: abortController.signal
}
).then(setData);
} catch (error) {
if ((error as Error).name === 'AbortError') {
console.log('Request was aborted');
} else {
setError(error);
}
}
return () => {
abortController.abort();
};
}, [client]);
[!NOTE]
Using observable streams, the request is aborted when the request is no longer observed.
useEffect(() => {
const sub = client.json$('/api').subscribe({
next: setData,
error: setError
});
return () => sub.unsubscribe();
}, [client]);
[!TIP]
HttpClient.abort
will cancel all ongoing requests.
Utilize request and response operators
The HTTP client provides the ability to add custom request and response operators. These operators allow you to intercept and modify requests before they are sent and responses before they are processed.
Functionality:
- Collection Management: Operators can add, set, get, and manage a collection of operators.
- Chaining: Operators are processed in sequence, allowing for a chain of modifications.
- Reusable: Operators can be shared across different operators.
- Extensible: Custom operators can be created for specific needs.
type ProcessOperator<T, R = T> = (request: T) => R | void | Promise<R | void>;
interface IProcessOperators<T> {
add: (name: string, operator: ProcessOperator<T>) => void;
set: (name: string, operator: ProcessOperator<T>) => void;
get: (name: string) => ProcessOperator<T> | undefined;
remove: (name: string) => void;
}
[!CAUTION]
Even though the process operator can return a value, it is not recommended to do so. This can cause unexpected behavior.
[!WARNING]
- Handlers are permanent to the client instance.
add
will throw error if a handler with the same name already exists.set
will override existing handlers with the same name.
[!IMPORTANT]
Handlers are executed in the order they are added. This means that you should add handlers that are more specific to the end of the list.
Request Handlers
You can add request handlers to modify outgoing requests:
client.requestHandler.setHeader('X-Custom-Header', 'CustomValue');
client.requestHandler.add(
'request-logger',
(request) => {
console.debug('Outgoing request:', request.url);
}
);
Available Request Handlers
capitalizeRequestMethodOperator
operator to ensure that the HTTP method of a given request is in uppercase.
[!NOTE]
by default this plugin will log a warning if the method was not in uppercase.
This can be disabled by setting the silent
option to true
.
import { capitalizeRequestMethodOperator } from '@equinor/fusion-framework-module-http/operators';
client.requestHandler.add(
'capatalize-method',
capitalizeRequestMethodOperator()
);
client.get('https://example.com', { method: 'get' });
requestValidationOperator
operator to validate the request before it is sent.
[!NOTE]
By default this plugin will only log a warning if the request is invalid.
To allow the plugin to modify FetchRequest
set the parse
option to true
.
NOTE this will also throw an error if the request is invalid.
import { requestValidationOperator } from '@equinor/fusion-framework-module-http/operators';
client.requestHandler.add(
'validate-request',
requestValidationOperator(
{
parse: true,
strict: false,
}
)
);
[!WARNING]
Validating RequestInit
should not be necessary, but a helpful tool for development.
Response Handlers
[!IMPORTANT]
Response operators should not modify the response object directly, this might lead to unexpected behavior, like providing the wrong response to the next operator and the provided response selector.
2nd, there are no good way to infer the response type, so the response object should be returned as is.
intercept and modify responses before they are processed:
client.responseHandler.add(
'response-logger',
(response) => {
console.log('Incoming response:', response.url, response.status);
}
);
client.responseHandler.add(
'response-validator'
(response) => {
if (response.status === 401) {
throw Error('response was 401');
}
}
);
Executing Calls
The HTTP client provides several methods to execute fetch calls. Here are some examples:
client.fetch('/users');
client.fetch$('/users');
client.json<Users>('/users');
client.json$<Users>('/users');
client.json<BlogPost>('/posts', {
method: 'POST',
body: { title: 'New Post', content: 'Content here' },
})
client.blob('/image.jpg').then(
({ filename, blob }) => {
const url = URL.createObjectURL(blob);
return `<a download='${filename}' href='${url}'>`
}
);
client.execute<Users>('json', '/users');
Monitoring HTTP Activity
The HTTP client provides observables that allow you to monitor all incoming requests and responses. This can be useful for logging, debugging, and tracking network activity in your application.
client.request$.subscribe(request => {
console.log('Incoming request:', request);
});
client.response$.subscribe(response => {
console.log('Incoming response:', response);
});